#!/bin/bash

## /Config
	#### /Quality
		# livestreamer: worst,medium,best,source,360p,720p,1080p
		# youtube-dl: 144,320,480,720,1080
		qualYT="manual"
		qualTW="manual"
		qualDF="best"
	#### \Quality
	#### /Player
		player="/usr/bin/vlc -I dummy"
	#### \Player
## \Config

## Youtube
if [ -n $(echo $URL | grep "youtube.com") ] || [ -n $(echo $URL | grep "youtu.be") ]; then
	if [ "$qualYT" = "manual" ]; then
		qualopt=$(mktemp)
		echo "144" >> $qualopt # WORKS ONLY WITH YOUTUBE-DL
		echo "360" >> $qualopt # WORKS ONLY WITH YOUTUBE-DL
		echo "480" >> $qualopt # WORKS ONLY WITH YOUTUBE-DL
		echo "720" >> $qualopt # WORKS ONLY WITH YOUTUBE-DL
		echo "1080" >> $qualopt # WORKS ONLY WITH YOUTUBE-DL
		qualYT=$(cat "$qualopt" | dmenu -p "Choose Quality of replay" -fn "-xos4-terminus-medium-r-*-*-28-*" -b)
		rm "$qualopt"
	fi
	#livestreamer -p "$player" $1 $qualYT
	youtube-dl -o - -f "best[height<=$qualYT]" "$1" | $player - 

## Twitch.tv
elif [ -n $(echo $URL | grep "twitch.tv") ]; then
	if [ "$qualTW" = "manual" ]; then
		qualopt=$(mktemp)
		echo "worst" >> $qualopt # WORKS ONLY WITH LIVESTREAMER
		echo "medium" >> $qualopt # WORKS ONLY WITH LIVESTREAMER
		echo "best" >> $qualopt # WORKS ONLY WITH LIVESTREAMER
		echo "source" >> $qualopt # WORKS ONLY WITH LIVESTREAMER
		qualTW=$(cat "$qualopt" | dmenu -p "Choose Quality of replay")
		rm "$qualopt"
	fi
	livestreamer -p "$player" "$1" $qualTW

## Default
else
	if [ "$2" = "raw" ]; then
		$player "$1"

	else
		if [ "$qualDF" = "manual" ]; then
			qualopt=$(mktemp)
			echo "worst" >> $qualopt # WORKS ONLY WITH LIVESTREAMER
			echo "best" >> $qualopt # WORKS ONLY WITH LIVESTREAMER
			qualDF=$(cat "$qualopt" | dmenu -p "Choose Quality of replay")
			rm "$qualopt"
		fi
		livestreamer -p $player "$1" $qualDF
	fi
	#youtube-dl -o - -f "best" "$1" | $player -
fi
